package com.smzd.framework.oauth2.business;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

import com.smzd.core.dao.IDao;
import com.smzd.core.domain.Many2Many;
import com.smzd.framework.core.business.AbstractFGService;
import com.smzd.framework.oauth2.dao.ClientAppDao;
import com.smzd.framework.oauth2.domain.ClientApp;
import com.smzd.framework.utils.db.FGRowConverter;

@Service
public class ClientAppService extends AbstractFGService<ClientApp> {
    @Autowired
    private ClientAppDao dao;

    @Override
    protected IDao<ClientApp> getDao() {
        return dao;
    }

    @Override
    public FGRowConverter<ClientApp> getConverter() {
        return ClientAppService.converter;
    }

    private static FGRowConverter<ClientApp> converter = new FGRowConverter<ClientApp>() {
        @Override
        public Object[] convert(ClientApp elem) {
            String scopeStr = elem.getScope() == null ? null : elem.getScope().toString();
            String resourceStr = elem.getResourceIds() == null ? null : elem.getResourceIds().toString();
            String authorityStr = null;
            ;
            if (elem.getAuthorities() != null && elem.getAuthorities().size() > 0) {
                List<String> authorities = new ArrayList<String>();
                for (GrantedAuthority authority : elem.getAuthorities()) {
                    authorities.add(authority.getAuthority());
                }
                authorityStr = authorities.toString();
            }
            Object[] fields = new Object[] {
                    elem.getId(),
                    elem.getName(),
                    elem.getClientId(),
                    scopeStr,
                    resourceStr,
                    authorityStr,
                    elem.getDescription()
            };
            return fields;
        }
    };

    public void saveAppClientResources(int clientAppId, String resourceIds) {
        String[] ids = resourceIds.split(",");
        List<Many2Many> toAdd = new ArrayList<Many2Many>();
        List<Many2Many> toDel = new ArrayList<Many2Many>();
        for (String id : ids) {
            int rid = Integer.parseInt(id);
            Many2Many mm = new Many2Many();
            mm.setKey1(clientAppId);
            if (rid < 0) {
                mm.setKey2(-rid);
                toDel.add(mm);
            } else {
                mm.setKey2(rid);
                toAdd.add(mm);
            }
        }
        dao.saveAppClientResources(toAdd, toDel);
    }

    public void saveAppClientAuthorities(int clientAppId, String authIds) {
        String[] ids = authIds.split(",");
        List<Many2Many> toAdd = new ArrayList<Many2Many>();
        List<Many2Many> toDel = new ArrayList<Many2Many>();
        for (String id : ids) {
            int rid = Integer.parseInt(id);
            Many2Many mm = new Many2Many();
            mm.setKey1(clientAppId);
            if (rid < 0) {
                mm.setKey2(-rid);
                toDel.add(mm);
            } else {
                mm.setKey2(rid);
                toAdd.add(mm);
            }
        }
        dao.saveAppClientAuthorities(toAdd, toDel);
    }

    public ClientApp getClientAppByClientId(String clientId) {
        return dao.getClientAppByClientId(clientId);
    }
}
